package cn.xdf.controller;

import cn.xdf.entity.City;
import cn.xdf.entity.Province;
import cn.xdf.entity.Student;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

//@Controller+@ResponseBody//将结果集以json形式响应
@RequestMapping("/demo01")
@RestController
public class Demo01Controller {

    @GetMapping("/demo01")
    public City demo01()throws Exception{

        City city=new City(1,"南昌",5500.0,true);
        return city; //返回json形式
    }


    @GetMapping(value = "/demo02")
    public List<City> demo02()throws Exception{

        List<City> cityList=new ArrayList<>();
        cityList.add(new City(1,"南京",14000.00,true));
        cityList.add(new City(2,"南通",2400.00,false));
        cityList.add(new City(3,"南阳",4000.00,false));
        return cityList;
    }


    @GetMapping(value = "/demo03")
    public String demo03() throws Exception{
        return "<h1>hello</h1>";//响应的信息被放在响应体中，而不是当做视图跳转
    }


    @GetMapping(value = "/demo04")
    public String demo04() throws Exception{
        return "hello world";//响应的信息被放在响应体中，而不是当做视图跳转
    }

    @ResponseBody           // 将结果集以json形式响应
    @RequestMapping(value = "/demo05")
    public City demo05() throws Exception{

        City city = new City(1, "南昌", 5500.0, true,new Province(1,"江西"));
        return city;
    }


    @ResponseBody
    @RequestMapping(value = "/demo06")
    public Student demo06()throws Exception{
        return new Student(1,"zhangsan",20,new Date());
        //new date()是一个毫秒值
    }


    /**
     * 响应ResponseEntity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/demo07")
    public ResponseEntity<City> demo07() {

        // 响应体
        City city = new City(1, "南宁", 5000.0D, true, new Province(1, "广西"));

        // 响应头
        HttpHeaders headers = new HttpHeaders();
        headers.put("test", Arrays.asList("abc"));
        headers.put("username",Arrays.asList("xiaohui","xiaolan"));

        // 创建一个响应报文,相应头,响应体，响应行
        ResponseEntity<City> responseEntity = new ResponseEntity<>(city, headers, HttpStatus.OK);

        return responseEntity;
    }



}
